
package com.csc440.team2.smil;

import java.io.IOException;
import java.io.StringReader;


import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;


import org.xml.sax.Attributes;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.XMLReader;
import org.xml.sax.helpers.DefaultHandler;


public class SMILReader extends DefaultHandler
{
    private SMILMessage currentMessage;
    private SMILComponent currentComponent;
    private boolean readyForText;


    public SMILReader()
    {
        super();
    }


    public static SMILMessage parseMessage(String input)
    {
        try
        {
            SAXParserFactory spf = SAXParserFactory.newInstance();
            SAXParser sp = spf.newSAXParser();
            XMLReader xr = sp.getXMLReader();
            SMILReader parser = new SMILReader();
            xr.setContentHandler(parser);
            xr.setErrorHandler(parser);

            xr.parse(new InputSource(new StringReader(input)));

            SMILMessage message = parser.getMessage();

            return message;
        }
        catch (SAXException e)
        {
            e.printStackTrace();
        }
        catch (IOException e)
        {
            e.printStackTrace();
        }
        catch (ParserConfigurationException e)
        {
            e.printStackTrace();
        }

        return null;
    }


    /*
     * (non-Javadoc)
     * 
     * @see org.xml.sax.helpers.DefaultHandler#endDocument()
     */
    @Override
    public void endDocument() throws SAXException
    {
        System.out.println("End of document.");
    }


    /*
     * (non-Javadoc)
     * 
     * @see org.xml.sax.helpers.DefaultHandler#startDocument()
     */
    @Override
    public void startDocument() throws SAXException
    {
        System.out.println("Start of document.");
        currentMessage = SMILMessage.create();
    }


    @Override
    public void startElement(String uri, String name, String qName, Attributes atts)
    {
        readyForText = false;

        if ("text".equalsIgnoreCase(name))
        {
            SMILTextComponent component = SMILTextComponent.create();

            component.setDuration(Integer.parseInt(atts.getValue("dur")));

            currentComponent = component;

            currentMessage.addComponent(currentComponent);
            readyForText = true;
        }

        if ("".equals(uri))
            System.out.println("Start element: " + name);
        else
            System.out.println("Start element: {" + uri + "}" + name);

    }


    @Override
    public void endElement(String uri, String name, String qName)
    {
        if ("".equals(uri))
            System.out.println("End element: " + qName);
        else
            System.out.println("End element:   {" + uri + "}" + name);
    }


    @Override
    public void characters(char ch[], int start, int length)
    {
        StringBuffer buffer = new StringBuffer();
        for (int i = start; i < start + length; i++)
        {

            buffer.append(ch[i]);

        }

        if (readyForText && currentComponent instanceof SMILTextComponent)
        {
            readyForText = false;
            SMILTextComponent textComponent = (SMILTextComponent) currentComponent;
            textComponent.setText(buffer.toString());
        }
    }


    public SMILMessage getMessage()
    {
        return currentMessage;
    }

}
